<template>
  <a-card :bordered="false" class="card-area">
    <a-form :form="form">
      <a-row>
        <a-col :md="6" :sm="24">
          <a-form-item
            label="学号"
            :labelCol="{span: 7,offset: 1}"
            :wrapperCol="{span: 15, offset: 1}"
          >
            <a-input v-decorator="['xh']"/>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item
            label="所在年级"
            :labelCol="{span: 7,offset: 1}"
            :wrapperCol="{span: 15, offset: 1}"
          >
            <a-select v-decorator="['nj',
              {
                rules: [{ required: true, message: '请选择年级!' }]
              }]" allowClear>
              <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item
            label="学年"
            :labelCol="{span: 7,offset: 1}"
            :wrapperCol="{span: 15, offset: 1}"
          >
            <a-select v-decorator="['xn',
              {
                rules: [{ required: true, message: '请选择学年!' }]
              }]" allowClear>
              <a-select-option v-for="i in TYPE0125" :key="i.id" :value="String(i.code)">{{ i.name }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item
            label="学期"
            :labelCol="{span: 7,offset: 1}"
            :wrapperCol="{span: 15, offset: 1}"
          >
            <a-select v-decorator="['xq',
              {
                rules: [{ required: true, message: '请选择学期!' }]
              }]" allowClear>
              <a-select-option v-for="i in TYPE0072" :key="i.id" :value="String(i.code)">{{ i.name }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row style="text-align:center">
        <a-button :loading="loading" type="primary" @click="saveForm">初始化</a-button>
      </a-row>
    </a-form>
  </a-card>
</template>
<script>
import { mapState } from 'vuex'

const selectArr = ['TYPE0126', 'TYPE0072', 'TYPE0125']
export default {
  data () {
    return {
      queryForm: {},
      studentInfo: {},
      selectMenu: {},
      loading: false
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'], // 学期
      TYPE0072: state => state.app['TYPE0072'], // 学期
      TYPE0125: state => state.app['TYPE0125'] // 学年
    })
  },
  created () {
    this.initDictionary()
    this.$store.dispatch('app/sysInfo').then(res => {
      this.form.setFieldsValue({xn: this.systemInfo.schoolYear, xq: this.systemInfo.currentSemester})
    })
    // this.initSelectMenu()
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  methods: {
    initDictionary () {
      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })
    },
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$api.student.teacherQueryAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data,
          tutor: res[1].data
        }
      })
    },
    yxChange (code) {
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    saveForm () {
      this.form.validateFields((err, values) => {
        console.log(values)
        if (err) {
          return
        }
        this.loading = true
        let params = Object.assign(values)
        this.$api.student.registerInitialByBatch(params).then(res => {
          this.$message.success(res.msg)
        }).finally(() => {
          this.loading = false
        })
      })
    }
  }
}
</script>
